package com.naja.event.tracking.common.config;

import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
import com.clickhouse.client.config.ClickHouseClientOption;
import com.clickhouse.client.http.config.ClickHouseHttpOption;
import com.clickhouse.client.http.config.HttpConnectionProvider;
import com.clickhouse.jdbc.ClickHouseDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * @auther wangjianying
 * @date 2023/08/04 17:46
 */
@Configuration
public class DynamicDatasourceConfig implements InitializingBean {
    @Autowired
    private DataSource dataSource;
    @Autowired
    private DynamicDataSourceProperties dynamicDataSourceProperties;
    @Autowired
    private MybatisPlusAutoConfiguration mybatisPlusAutoConfiguration;
    @Autowired
    private ApplicationContext applicationContext;

    /**
     *     ASYNC("async", true, "Whether the client should run in async mode."),
     *     AUTO_SESSION("auto_session", true, "Whether to create session automatically when there are multiple queries."),
     *     BUFFERING("buffering", ClickHouseDataConfig.DEFAULT_BUFFERING_MODE, "Buffering mode."),
     *     HOST("host", "localhost", "Host to connect to."),
     *     PROTOCOL("protocol", ClickHouseProtocol.ANY, "Protocol to use."),
     *     DATABASE("database", "default", "Database to connect to."),
     *     USER("user", "default", "User name for authentication."),
     *     PASSWORD("password", "", "Password for authentication.", true),
     *     FORMAT("format", ClickHouseDataConfig.DEFAULT_FORMAT, "Preferred data format for serialization and deserialization."),
     *     MAX_SCHEDULER_THREADS("max_scheduler_threads", 1, "Maximum number of threads that the scheduler(shared by all client instances) can use to run the adhoc/scheduled tasks like discovery and health check.."),
     *     MAX_THREADS("max_threads", 0, "Maximum size of shared thread pool, 0 or negative number means same as CPU cores."),
     *     MAX_REQUESTS("max_requests", 0, "Maximum size of shared thread pool, 0 means no limit."),
     *     ROUNDING_MODE("rounding_mode", ClickHouseDataConfig.DEFAULT_ROUNDING_MODE, "Default rounding mode for BigDecimal."),
     *     THREAD_KEEPALIVE_TIMEOUT("thread_keepalive_timeout", 0L, "Thread keep alive timeout in milliseconds. 0 or negative number means additional thread will be closed immediately after execution completed."),
     *     SERVER_TIME_ZONE("time_zone", "UTC", "Server time zone."),
     *     SERVER_VERSION("version", "latest", "Server version"),
     *     SSL_CERTIFICATE_TYPE("sslcerttype", "X.509", "SSL/TLS certificate type."),
     *     SSL_KEY_ALGORITHM("sslkeyalg", "RSA", "Key algorithm."),
     *     SSL_PROTOCOL("sslprotocol", "TLS", "SSL protocol."),
     *     SRV_RESOLVE("srv_resolve", false, "Whether to resolve DNS SRV name.");
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        DataSourceProperty clickhouse1 = dynamicDataSourceProperties.getDatasource().get("clickhouse_1");
        Properties properties = new Properties();
        properties.put("user",clickhouse1.getUsername());
        properties.put("password",clickhouse1.getPassword());
        properties.put(ClickHouseClientOption.SOCKET_KEEPALIVE.getKey(),true);
        properties.put(ClickHouseClientOption.SOCKET_TIMEOUT.getKey(),5*60*1000);
        properties.put(ClickHouseHttpOption.CONNECTION_PROVIDER.getKey(), HttpConnectionProvider.APACHE_HTTP_CLIENT);
        properties.put(ClickHouseHttpOption.KEEP_ALIVE.getKey(), true);
        ClickHouseDataSource clickHouseDataSource = new ClickHouseDataSource(clickhouse1.getUrl(), properties);
        ((DynamicRoutingDataSource) dataSource).addDataSource("clickhouse_1", clickHouseDataSource);
    }
    @Bean
    @Primary
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        return mybatisPlusAutoConfiguration.sqlSessionFactory(dataSource);
    }

    @Bean
    @Primary
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        return mybatisPlusAutoConfiguration.sqlSessionTemplate(sqlSessionFactory);
    }

    public static void main(String[] args) {
        System.out.println("a.b".split("\\.")[0]);
    }
}
